# Base image
FROM mcr.microsoft.com/windows/server:ltsc2022 as runtime

# Set the shell to PowerShell
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; Set-ExecutionPolicy Unrestricted -Force;"]

# Setup ASP.Net & IIS
RUN Add-WindowsFeature Web-Server

RUN dism /online /enable-feature /all /featurename:IIS-ASPNET45

# Disable IIS auto start
RUN ["cmd", "/S", "/C", "sc", "config", "w3svc", "start=demand"]

# Install LogMonitor.exe
RUN md c:\LogMonitor
COPY /tools/logmonitor/. c:/LogMonitor

WORKDIR /	

# Copy and extract IBM zip file
COPY tools/CLIENT.zip C:/IBM/
RUN Expand-Archive -Path 'C:/IBM/CLIENT.zip' -DestinationPath 'C:/IBM/' -Force

RUN Start-Process -FilePath 'C:\IBM\CLIENT\setup.exe' -ArgumentList '/u', 'C:\IBM\CLIENT\resp.rsp','/l','C:\IBM\CLIENT\log.txt','/t','C:\IBM\CLIENT\trace.txt' -Wait

# Ensure the DefaultAppPool is using the correct .NET Framework version
RUN c:/windows/system32/inetsrv/appcmd.exe set apppool /apppool.name:DefaultAppPool /processModel.identityType:LocalSystem

# Install service monitor
WORKDIR /
RUN Invoke-WebRequest -Uri "https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.10/ServiceMonitor.exe" -OutFile "ServiceMonitor.exe" -UseBasicParsing

# Update IIS config
RUN c:/windows/system32/inetsrv/appcmd.exe set config -section:system.applicationHost/sites /\"[name='Default Web Site'].logFile.logTargetW3C:\"File,ETW\"\" /commit:apphost

# Set the entry point for the container
ENTRYPOINT ["C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc"]